<template>
  <view>
    <!-- 搜索 -->
    <search />
    <!-- 分类 -->
    <view class="category">
      <!-- 顶级分类 -->
      <view class="sup">
        <!-- 区域滚动 -->
        <scroll-view scroll-y>
          <!-- 分类一级菜单 -->
          <text
            @click="switchCate(index)"
            :class="{active:active === index}"
            :key="itemOne.cat_id"
            v-for="(itemOne,index) in cates"
          >{{itemOne.cat_name}}</text>
        </scroll-view>
      </view>
      <!-- 子级分类 -->
      <view class="sub">
        <!-- 区域滚动 -->
        <scroll-view scroll-y>
          <!-- 封面图 -->
          <image src="http://static.botue.com/ugo/uploads/category.png" class="thumb" />
          <!-- 二级和三级分类 -->
          <!-- 二级 -->
          <view class="children" :key="subTwo.cat_id" v-for="subTwo in subs">
            <view class="title">{{subTwo.cat_name}}</view>
            <view class="brands">
              <!-- 品牌 三级 -->
              <navigator
                :url="`/packone/list/index?query=${subThree.cat_name}`"
                :key="subThree.cat_id"
                v-for="subThree in subTwo.children"
              >
                <image :src="subThree.cat_icon" />
                <text>{{subThree.cat_name}}</text>
              </navigator>
            </view>
          </view>
        </scroll-view>
      </view>
    </view>
  </view>
</template>

<script>
import search from "@/components/search"

export default {
  data () {
    return {
      // 分类数据
      cates: [],
      // 当前选中的一级的索引(默认是第一个)
      active: 0
    }
  },
  computed: {
    // 二级和三级分类数据
    subs () {
      return this.cates.length && this.cates[this.active].children
    }
  },
  components: {
    search
  },
  onLoad () {
    this.getCates()
  },
  methods: {
    // 获取分类数据
    async getCates () {
      const { msg: { status }, data } = await this.request({ url: '/api/public/v1/categories' })
      if (status === 200) {
        console.log(data)
        this.cates = data
      }
    },
    // 切换一级分类
    switchCate (i) {
      // console.log(i)
      this.active = i
    }
  },
};
</script>

<style scoped lang="scss">
scroll-view {
  height: 100%;
}

.category {
  display: flex;
  width: 100%;
  position: absolute;
  top: 100rpx;
  bottom: 0;

  .sup {
    width: 196rpx;
    background-color: #f4f4f4;

    text {
      display: block;
      height: 100rpx;
      text-align: center;
      line-height: 100rpx;
      font-size: 27rpx;
      color: #333;
      border-bottom: 1rpx solid #eee;

      &:last-child {
        border-bottom: none;
      }

      &.active {
        background-color: #fff;
        color: #ea4451;
        position: relative;

        &::before {
          content: "";
          display: block;
          width: 8rpx;
          height: 60rpx;
          transform: translateY(-50%);
          background-color: #ea4451;

          position: absolute;
          left: 0;
          top: 50%;
        }
      }
    }
  }

  .sub {
    flex: 1;
    padding: 20rpx 18rpx;

    .thumb {
      width: 100%;
      height: 180rpx;
    }

    .children {
      text-align: center;
      color: #333;

      .title {
        display: inline-block;
        margin: 40rpx 0 20rpx;
        font-size: 30rpx;

        &::before {
          content: "/";
          margin-right: 20rpx;
          color: #666;
        }

        &::after {
          content: "/";
          margin-left: 20rpx;
          color: #666;
        }
      }
    }

    .brands {
      display: flex;
      flex-wrap: wrap;

      navigator {
        width: 33%;
        margin-bottom: 20rpx;
      }

      image {
        width: 120rpx;
        height: 120rpx;
      }

      text {
        display: block;
        font-size: 24rpx;
      }
    }
  }
}
</style>
